<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
/* 001-面向对象编程之继承(混入式继承) */
        // let o = {
        //     name: "ws",
        //     age: "18",
        //     show() {
        //         console.log("我的名字是ws");

        //     }
        // }
        //获取到o的方法和属性
        //方案一：
        // let t = {};
        // for (let key in o) {
        //     t[key] = o[key];
        // }
        //方案二：
        // Object.assign(t,o)
        // console.log(t);

 /* 002-面向对象编程之继承(原型式继承) */
        //作用：继承父构造函数的原型对象
        //  缺点：无法继承父构造函数的实例对象&& d1.constructor == Dog => false
        let Animal = function (color, age) {
            this.color = color;
            this.age = age;
        }
        Animal.prototype = {
            constructor: Animal,
            run() {
                console.log("run");

            },
            eat() {
                console.log("eat");

            }
        }
        let Dog = function (name) {
            this.name = name;
        }

        /*获取原型对象的方法： Dog.prototype = Animal.prototype;*/
        //  Dog.prototype = Animal.prototype;
        //  let d1 = new Dog("wc");
        //  console.log(d1);

/* 003-面向对象编程之继承(原型链继承) */
        /*原型链继承：设置子构造函数的原型对象为父构造函数*/
        /*作用：可以继承父构造函数的实例对象和原型成员*/
        /*缺点：但是无法向父构造函数传参*/

        //  Dog.prototype = new Animal("yellow",3);
        //  Dog.prototype.yaoRen = function(){
        //      console.log("yaoren");

        //  }
        //  let d1 = new Dog("wc");
        //  console.log(d1);
        //  let d2 = new Dog("xq");
        //  console.log(d2);

/* 004-面向对象编程之继承(借用构造函数继承) */
        /* 作用：获取了父构造函数实例成员 */
        /* 缺点：无法获得原型成员 */

        let Animal = function (color, age) {
            this.color = color;
            this.age = age;
        }
        Animal.prototype = {
            constructor: Animal,
            run() {
                console.log("run");
            },
            eat() {
                console.log("eat");
            }
        }

        let Dog = function (name, color, age) {
            this.name = name;
            Animal.call(this, color, age);
        }

        Dog.prototype = Animal.prototype;

 /* 005-组合继承：借用父构造函数继承(实例成员) + 原型式继承(原型成员) */
        Dog.prototype.yaoRen = function () {
            console.log("yaoren");
            let d1 = new Dog("wc","yellow",4);
            console.log(d1);
            let d2 = new Dog("xq","green",3);
            console.log(d2);
        }

/*006-class类实现继承*/
            //父类
            class Animal {
                constructor(color,age){
                    this.color = color;
                    this.age = age;
                }
                run(){}
                eat(){}
            }   
            //子类
            /*
            1）通过extends关键字来实现class的继承
            2）必须通过super(color,age)来先调用父狗杂函数，之后才能使用this
            */
            class Dog extends Animal{//注意
                constructor(name,color,age){
                    super(color,age);//注意
                    this.name = name;
                }
                yaoRen(){}
            }
            let d1 = new Dog("wc", "红色", 3);
            let d2 = new Dog("xq", "黑色", 8);
            console.log(d1, d2);
    </script>
</body>

</html>